package cn.jaye.fabric.application;

import cn.jaye.fabric.application.command.LoginCommand;
import cn.jaye.fabric.application.command.RegistCommand;
import cn.jaye.fabric.common.constant.ExceptionEnum;
import cn.jaye.fabric.common.constant.MicroConstant;
import cn.jaye.fabric.common.exception.ServiceException;
import cn.jaye.fabric.common.model.SystemSession;
import cn.jaye.fabric.common.util.MD5Util;
import cn.jaye.fabric.domain.model.User;
import cn.jaye.fabric.infrastructure.mybatis.impl.UseRepositoryImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Jaye
 * @create 2018-06-04 11:29
 */
@Service
public class AuthApplicationService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private UseRepositoryImpl useRepository;

    @Autowired
    private MicroConstant microConstant;

    public boolean login(LoginCommand loginCommand) {
        User user = useRepository.selectOne(new EntityWrapper<User>().eq("user_name", loginCommand.getUsername()));
        if (null != user) {
            if (user.getPassword().equals(MD5Util.MD5(loginCommand.getPassword()))) {
//                SystemSession.setUser(user);redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS);
//                redisTemplate.opsForValue().set(loginCommand.getUsername(),
//                        (String) loginCommand.getUsername(),
//                        microConstant.getSessionTimes(), TimeUnit.SECONDS);
                return true;
            } else {
                throw new ServiceException(ExceptionEnum.USER_PASSWORD_ERROR);
            }
        }
        throw new ServiceException(ExceptionEnum.USER_NOT_REGISTER);
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean register(RegistCommand registCommand) throws InterruptedException {
        String username = registCommand.getUsername();
        User user = useRepository.selectOne(new EntityWrapper<User>().eq("user_name", username));
        if (null != user) {
            throw new ServiceException(ExceptionEnum.USER_REGISTER);
        }
        String password = MD5Util.MD5(registCommand.getPassword());
        User my = new User();
        my.setUserName(username);
        my.setPassword(password);
        my.setVoucherCode(registCommand.getVoucherCode());
        if (useRepository.insert(my)) {
            return true;
//            String[] args = {username};
//            JSONObject result = blockChainService.invoke(Global.CHAINCODE_FUNC_REGIST, args);
//            if (result.getInteger("code").equals(1000)) {
//                return true;
//            } else {
//                throw new ServiceException(ExceptionEnum.USER_REGISTER_ACCOUNT_FAILED);
//            }
        }
        return false;
    }
}
